﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bll;
using model;
using System.Web.Security;

public partial class PEdit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        if (Session["usergroup"].ToString() == "student")
        {
            StudentBll bll = new StudentBll();
            try
            {

                string sno = Session["userid"].ToString();
                Student student = bll.GetSingle(sno);
                if (student.Spwd == FormsAuthentication.HashPasswordForStoringInConfigFile(txtopwd.Text.Trim(),"MD5"))
                {
                    if (txtnpwd.Text.Trim() == txtnpwd2.Text.Trim())
                    {
                        string spwd = FormsAuthentication.HashPasswordForStoringInConfigFile(txtnpwd.Text.Trim(),"MD5");
                        bll.PwdUpdate(spwd, sno);
                        Response.Write("<script>alert('修改成功！');location.href='Default.aspx';</script>");
                    }
                    else
                    {
                        Response.Write("<script>alert('两次密码不一致！');history.back();</script>");
                    }
                }
                else
                {
                    Response.Write("<script>alert('原始密码不正确！');history.back();</script>");
                }
            }
            catch (Exception ex)
            {
                string js = "<script>alert('" + ex.Message + "');</script>";
                Response.Write(js);
            }
        }
        if (Session["usergroup"].ToString() == "enterprise")
        {
            EnterpriseBll bll = new EnterpriseBll();
            try
            {
                string eno = Session["userid"].ToString();
                Enterprise enterprise = bll.GetSingle(eno);
                if (enterprise.Epwd == FormsAuthentication.HashPasswordForStoringInConfigFile(txtopwd.Text.Trim(),"MD5"))
                {
                    if (txtnpwd.Text.Trim() == txtnpwd2.Text.Trim())
                    {
                        enterprise.Epwd = FormsAuthentication.HashPasswordForStoringInConfigFile(txtnpwd.Text.Trim(),"MD5");
                        bll.PwdUpdate(enterprise);
                        Response.Write("<script>alert('修改成功！');location.href('Default.aspx');</script>");
                    }
                    else
                    {
                        Response.Write("<script>alert('两次密码不一致！');history.back();</script>");
                    }
                }
                else
                {
                    Response.Write("<script>alert('原始密码不正确！');history.back();</script>");
                }
            }
            catch (Exception ex)
            {
                string js = "<script>alert('" + ex.Message + "');</script>";
                Response.Write(js);
            }
        }
        if (Session["usergroup"].ToString() == "admin")
        {
            AdminBLL bll = new AdminBLL();
            string pwd=bll.getAdminPwd();
            if ( pwd== FormsAuthentication.HashPasswordForStoringInConfigFile(txtopwd.Text.Trim(), "MD5"))
            {
                if (txtnpwd.Text.Trim() == txtnpwd2.Text.Trim())
                {
                    if (txtnpwd.Text.Trim() == "")
                    {
                        Response.Write("<script>alert('密码不能为空');history.back();</script>");
                        return;
                    }
                    pwd = FormsAuthentication.HashPasswordForStoringInConfigFile(txtnpwd.Text.Trim(), "MD5");
                    if (bll.PwdUpdate(pwd) > 0)
                    {
                        Response.Write("<script>alert('修改成功！');location.href('Default.aspx');</script>");
                    }
                    else
                    {
                        Response.Write("<script>alert('修改失败！');location.href('Default.aspx');</script>");
                    }
                }
                else
                {
                    Response.Write("<script>alert('两次密码不一致！');history.back();</script>");
                }
            }
            else
            {
                Response.Write("<script>alert('原始密码不正确！');history.back();</script>");
            }
        }
    }
   
}